package com.thh.datamonitor.controller;

import cn.hutool.core.bean.BeanUtil;
import com.thh.common.core.controller.BaseController;
import com.thh.common.core.model.AjaxResult;
import com.thh.common.utils.HitUtil;
import com.thh.common.utils.ServletUtils;
import com.thh.common.utils.ShiroUtils;
import com.thh.datamonitor.dto.*;
import com.thh.datamonitor.param.TmdTableInfoQueryParam;
import com.thh.datamonitor.param.TmdTableQueryParam;
import com.thh.datamonitor.entity.TmdDatabase;
import com.thh.datamonitor.executor.AbstractDbExecutor;
import com.thh.datamonitor.executor.DbExecutorFactory;
import com.thh.datamonitor.service.ITmdDatabaseService;
import com.thh.datamonitor.service.ITmdUserDatabaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据表Controller
 * 
 * @author thh
 * @date 2022-06-23
 */
@Controller
@RequestMapping("/datamonitor/table")
public class TableController extends BaseController
{
    private String prefix = "datamonitor/table";

    @Autowired
    private ITmdDatabaseService tmdDatabaseService;

    @Autowired
    private ITmdUserDatabaseService tmdUserDatabaseService;

    @Autowired
    DbExecutorFactory dbExecutorFactory;


    @RequiresPermissions("datamonitor:table:view")
    @GetMapping()
    public String TableMain(ModelMap mmap)
    {
        TmdUserDatabaseQueryDto tmdUserDatabaseQueryDto = new TmdUserDatabaseQueryDto();
        tmdUserDatabaseQueryDto.setUserId(ShiroUtils.getLoginName());
        List<TmdUserDatabaseDto> userDatabaseList = tmdUserDatabaseService.selectMyUserDatabaseList(tmdUserDatabaseQueryDto);
        mmap.put("myDatabaseList",userDatabaseList);
        return prefix + "/TableMain";
    }

    // 表信息页面
    @GetMapping( "/tablePage")
    public String loadPage(Model model){
        HttpServletRequest request = ServletUtils.getRequest();
        model.addAttribute("tableName", request.getParameter("tableName"));
        model.addAttribute("databaseId", request.getParameter("databaseId"));
        return prefix + "/tablePage";
    }

    /**
     * 查询表数据
     */
    @PostMapping("/tableInfo/list")
    @ResponseBody
    public AjaxResult infoList(TmdTableInfoQueryParam tmdTableQueryParam) throws Exception {
        String databaseId = tmdTableQueryParam.getDatabaseId();
        TmdDatabase database = tmdDatabaseService.getMyDatabaseId(databaseId);
        String databaseType = database.getDatabaseType();
        AbstractDbExecutor dbExecutor = dbExecutorFactory.getDbExecutor(databaseType);
        TableInfoListDto tableInfoListDto = dbExecutor.tableInfoListDto(database, tmdTableQueryParam);
        return AjaxResult.success(tableInfoListDto);
    }

    /**
     * 查询表列表
     */
    //@RequiresPermissions("datamonitor:table:list")
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(TmdTableQueryParam tmdTableQueryParam)
    {
        System.out.println("tmdTableQueryParam = " + tmdTableQueryParam);
        String databaseId = tmdTableQueryParam.getDatabaseId();
        TmdDatabase database = tmdDatabaseService.getMyDatabaseId(databaseId);
        String databaseType = database.getDatabaseType();
        AbstractDbExecutor dbExecutor = dbExecutorFactory.getDbExecutor(databaseType);
        List<TmdTableDto> tableList = dbExecutor.getTableList(database ,tmdTableQueryParam);

        List dbInfo = new ArrayList();
        Map mapTable = new HashMap();
        mapTable.put("id", database.getDatabaseDbname());
        mapTable.put("tableName", "库");

        mapTable.put("name", database.getDatabaseDbname());
        mapTable.put("iconCls", "icon-diy-folder1");
        List<Map<String, Object>> tableChild = new ArrayList();


        String keyword = tmdTableQueryParam.getTableName();
        for (TmdTableDto tmdTableDto : tableList) {
            Map<String, Object> data = BeanUtil.beanToMap(tmdTableDto);
            data.put("iconCls", "icon-diy-table1");
            data.put("id", tmdTableDto.getTableName());
            data.put("name", HitUtil.getHitCode(tmdTableDto.getTableName(), keyword));
            tableChild.add(data);
        }

        mapTable.put("children", tableChild);
        dbInfo.add(mapTable);
        return  AjaxResult.success("查询成功", dbInfo);
    }



    /**
     * 查询数据表列表
     */
    //@RequiresPermissions("datamonitor:table:ddl")
    @PostMapping("/ddl")
    @ResponseBody
    public AjaxResult ddl(TmdTableQueryParam tmdTableQueryParam)
    {
        TmdDatabase database = tmdDatabaseService.getMyDatabaseId(tmdTableQueryParam.getDatabaseId());
        AbstractDbExecutor dbExecutor = dbExecutorFactory.getDbExecutor(database.getDatabaseType());
        String ddl = dbExecutor.ddl(database, tmdTableQueryParam);
        return AjaxResult.success("查询成功", ddl);
    }

    /**
     * 查询数据表列表
     */
    //@RequiresPermissions("datamonitor:table:primaryKey")
    @PostMapping("/primaryKey")
    @ResponseBody
    public AjaxResult primaryKey(TmdTableQueryParam tmdTableQueryParam)
    {
        TmdDatabase database = tmdDatabaseService.getMyDatabaseId(tmdTableQueryParam.getDatabaseId());
        AbstractDbExecutor dbExecutor = dbExecutorFactory.getDbExecutor(database.getDatabaseType());
        String primaryKey = dbExecutor.getPrimaryKey(database, tmdTableQueryParam.getTableName());
        return AjaxResult.success("查询成功", primaryKey);
    }
}
